package showgo.gui;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;

import showgo.modell.Manager;
import showgo.modell.inszenierung.Darstellerposten;
import showgo.modell.inszenierung.Inszenierung;
import showgo.modell.inszenierung.Nichtdarstellerposten;
import showgo.modell.stamm.Ensemble;
import showgo.modell.stamm.Person;
import showgo.modell.stueck.Stueck;

public class ShowGoDialog extends JDialog implements ActionListener,
	KeyListener, ListSelectionListener, IMyListener {

    /**
     * 
     */
    private static final long serialVersionUID = 8627878302852141868L;
    private JFileChooser htmlFileChooser;
    private JFileChooser nnnFileChooser;

    private javax.swing.JButton projektButton;
    private javax.swing.JButton beendenButton;
    private javax.swing.JButton entferneStueckButton;
    private javax.swing.JButton detailStueckButton;
    private javax.swing.JButton personenButton;
    private javax.swing.JButton hilfeButton;
    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;
    private JScrollPane jScrollPane3;
    private javax.swing.JButton ladeStueckButton;
    @SuppressWarnings("rawtypes")
    private JList personList;
    private javax.swing.JButton cloneStueckButton;
    private javax.swing.JLabel stueckLabel;
    @SuppressWarnings("rawtypes")
    private JList stueckList;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JButton ensembleDetailButton;
    private javax.swing.JButton ensembleEntfButton;
    private javax.swing.JButton ensembleKlonenButton;
    private javax.swing.JLabel ensembleLabel;
    @SuppressWarnings("rawtypes")
    private JList ensembleList;
    private javax.swing.JButton ensembleNeuButton;
    private javax.swing.JLabel personenLabel;
    private javax.swing.JLabel inszLabel;
    private javax.swing.JButton inszenBearbButton;
    private javax.swing.JButton inszenEntfButton;
    @SuppressWarnings("rawtypes")
    private JList inszenList;
    private javax.swing.JButton inszenNeuButton;
    private javax.swing.JScrollPane inszenScrollPane;
    private javax.swing.JButton speichernButton;
    private javax.swing.JButton inszenPrHeftButton;

    public ShowGoDialog(int width, int height) {

	setTitle("The Show must go on!     Lutz Schneider - 8907870");
	setSize(width, height);
	setLayout(new BorderLayout());
	setLocationRelativeTo(null);

	Image image = Toolkit.getDefaultToolkit().getImage("resource/icon.png");
	setIconImage(image);

	initComponents();
	Manager.getInstance().addListener(this);
	erzeugeFileFilter();
	startTimer();
	Manager.getInstance().initNichtDarstellerPosten();

	// wurde Programm nicht korrekt beendet, soll
	// der Anwender die Moeglichkeit haben die
	// letzte Sicherung zu laden;
	if (!programmKorrektBeendet() && getDatumDerSicherung().length() > 1) {
	    int ret = JOptionPane
		    .showConfirmDialog(
			    null,
			    "Das Programm wurde zuletzt nicht korrekt beendet!\nWollen Sie die letzte Sicherung vom "
				    + getDatumDerSicherung() + " laden?",
			    "Hinweis!", JOptionPane.YES_NO_OPTION);
	    if (ret == JOptionPane.YES_OPTION) {
		Manager.getInstance().ladeProjekt("Projekt(temp).nnn");
		Manager.getInstance().fireListChanged();
	    }
	}
    }

    private String getDatumDerSicherung() {
	String datum = "-";
	// da es die Sicherung ja gibt, wird erst mal
	// die erste Zeile geladen
	try {
	    FileReader fr = new FileReader("Projekt(temp).nnn");
	    BufferedReader br = new BufferedReader(fr);
	    String zeile;
	    try {
		zeile = br.readLine();
		// dann wird aus dem geladenen String das Datum geholt
		datum = zeile.substring(zeile.indexOf(":") + 1,
			zeile.indexOf(":") + 24);
	    } catch (Exception e) {
		JOptionPane.showMessageDialog(null,
			"Die Sicherung konnte nicht geladen werden!");
	    }
	    try {
		br.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	    try {
		fr.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	}
	return datum;
    }

    private boolean programmKorrektBeendet() {
	// beim korrekten Beenden des Programms
	// durch den Benutze wird die Datensicherung
	// entfernt; ist diese Datei trotzdem da,
	// bedeutet dies, dass das Programm nicht
	// korrekt beendet wurde

	File file = new File("Projekt(temp).nnn");
	if (file.exists()) {
	    return false;
	}
	return true;
    }

    private void ladeProjekt() {
	// wenn schon Daten vorhanden sind, dann erst Fragen
	// ob die geaenderten Daten gesichert werden sollen;
	int ret = 0;
	if (!Manager.getInstance().getEnsembles().isEmpty()
		|| !Manager.getInstance().getPersonen().isEmpty()
		|| !Manager.getInstance().getStuecke().isEmpty()) {
	    ret = JOptionPane.showConfirmDialog(null,
		    "Wollen Sie die aktuellen Daten verwerfen?",
		    "Neues Projekt laden!", JOptionPane.YES_NO_OPTION);
	}
	int n = nnnFileChooser.showOpenDialog(this);
	if (n == JFileChooser.APPROVE_OPTION) {
	    File file = nnnFileChooser.getSelectedFile();
	    if (ret == JOptionPane.YES_OPTION) {
		Manager.getInstance().getEnsembles().clear();
		Manager.getInstance().getPersonen().clear();
		Manager.getInstance().getStuecke().clear();
		Manager.getInstance().getInszenierungen().clear();
	    }
	    Manager.getInstance().ladeProjekt(file.getPath());
	    Manager.getInstance().fireListChanged();
	}
    }

    private void startTimer() {
	// Timer fuer Datensicherung
	// nach einer Minute und dann jede weitere
	// Minute wird der komplette Datenbestand
	// gesichert - Projekt(temp).nnn
	Timer sicherung = new Timer();
	sicherung.schedule(new TimerTask() {
	    @Override
	    public void run() {
		Manager.getInstance().schreibeProjektInDatei(
			new File("Projekt"), true);
	    }
	}, 60000, 60000);
    }

    @SuppressWarnings("rawtypes")
    private void initComponents() {
	setPreferredSize(new java.awt.Dimension(1000, 520));
	setResizable(false);

	inszenScrollPane = new javax.swing.JScrollPane();
	inszenList = new JList();
	inszenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	inszenList.addListSelectionListener(this);
	inszenScrollPane.setViewportView(inszenList);

	inszenNeuButton = new javax.swing.JButton();
	inszenNeuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	inszenNeuButton.setText("Neue Inszen.");
	inszenNeuButton.addActionListener(this);

	inszenBearbButton = new javax.swing.JButton();
	inszenBearbButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	inszenBearbButton.setText("Bearbeiten");
	inszenBearbButton.addActionListener(this);

	inszenEntfButton = new javax.swing.JButton();
	inszenEntfButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	inszenEntfButton.setText("Entfernen");
	inszenEntfButton.addActionListener(this);

	inszLabel = new javax.swing.JLabel();
	inszLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
	inszLabel.setText("Inszenierungen");

	personenLabel = new javax.swing.JLabel();
	ensembleLabel = new javax.swing.JLabel();

	personenLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
	personenLabel.setText("Personen");

	ensembleLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
	ensembleLabel.setText("Ensemble");

	ensembleDetailButton = new javax.swing.JButton();
	ensembleDetailButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ensembleDetailButton.setText("Details");
	ensembleDetailButton.addActionListener(this);

	ensembleNeuButton = new javax.swing.JButton();
	ensembleNeuButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ensembleNeuButton.setText("Neu");
	ensembleNeuButton.addActionListener(this);

	ensembleKlonenButton = new javax.swing.JButton();
	ensembleKlonenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ensembleKlonenButton.setText("Klonen");
	ensembleKlonenButton.addActionListener(this);

	ensembleEntfButton = new javax.swing.JButton();
	ensembleEntfButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ensembleEntfButton.setText("Entfernen");
	ensembleEntfButton.addActionListener(this);

	titleLabel = new javax.swing.JLabel();
	titleLabel.setFont(new java.awt.Font("Tahoma", 1, 16));
	titleLabel.setText("ShowGO - Verwaltung von Theaterstücken");

	stueckLabel = new javax.swing.JLabel();

	entferneStueckButton = new javax.swing.JButton();

	beendenButton = new javax.swing.JButton();
	projektButton = new javax.swing.JButton();

	jScrollPane2 = new JScrollPane();
	personList = new JList();
	personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	jScrollPane2.setViewportView(personList);

	jScrollPane1 = new JScrollPane();
	stueckList = new JList();
	stueckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	stueckList.addListSelectionListener(this);
	jScrollPane1.setViewportView(stueckList);

	jScrollPane3 = new JScrollPane();
	ensembleList = new JList();
	ensembleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	ensembleList.addListSelectionListener(this);
	jScrollPane3.setViewportView(ensembleList);

	stueckLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
	stueckLabel.setText("vorhandene Stücke");

	ladeStueckButton = new javax.swing.JButton();
	ladeStueckButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ladeStueckButton.setText("Stück laden");
	ladeStueckButton.addActionListener(this);

	detailStueckButton = new javax.swing.JButton();
	detailStueckButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	detailStueckButton.setText("Stück bearbeiten");
	detailStueckButton.addActionListener(this);

	cloneStueckButton = new javax.swing.JButton();
	cloneStueckButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	cloneStueckButton.setText("Stück klonen");
	cloneStueckButton.addActionListener(this);

	entferneStueckButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	entferneStueckButton.setText("Stück löschen");
	entferneStueckButton.addActionListener(this);

	beendenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	beendenButton.setText("Beenden");
	beendenButton.addActionListener(this);

	projektButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	projektButton.setText("Projekt laden");
	projektButton.addActionListener(this);

	personenButton = new javax.swing.JButton();
	personenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	personenButton.setText("Personen bearbeiten");
	personenButton.addActionListener(this);

	hilfeButton = new javax.swing.JButton();
	hilfeButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	hilfeButton.setText("Hilfe");
	hilfeButton.addActionListener(this);

	speichernButton = new javax.swing.JButton();
	speichernButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	speichernButton.setText("Projekt speichern");
	speichernButton.addActionListener(this);

	inszenPrHeftButton = new javax.swing.JButton();
	inszenPrHeftButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	inszenPrHeftButton.setText("Programmheft");
	inszenPrHeftButton.addActionListener(this);

	// uebernommen von NetBeans
	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(
									beendenButton,
									javax.swing.GroupLayout.Alignment.TRAILING,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									155,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(
									hilfeButton,
									javax.swing.GroupLayout.Alignment.TRAILING,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									155,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(
									layout.createSequentialGroup()
										.addGap(530,
											530,
											530)
										.addGroup(
											layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
													speichernButton,
													javax.swing.GroupLayout.Alignment.TRAILING,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													155,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
													projektButton,
													javax.swing.GroupLayout.Alignment.TRAILING,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													155,
													javax.swing.GroupLayout.PREFERRED_SIZE))))
						.addGroup(
							layout.createSequentialGroup()
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(
											titleLabel)
										.addGroup(
											layout.createSequentialGroup()
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														false)
														.addGroup(
															layout.createSequentialGroup()
																.addComponent(
																	jScrollPane1,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	140,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(18,
																	18,
																	18)
																.addGroup(
																	layout.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING,
																		false)
																		.addComponent(
																			detailStueckButton,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			Short.MAX_VALUE)
																		.addComponent(
																			ladeStueckButton,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			Short.MAX_VALUE)
																		.addComponent(
																			cloneStueckButton,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			Short.MAX_VALUE)
																		.addComponent(
																			entferneStueckButton,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			javax.swing.GroupLayout.DEFAULT_SIZE,
																			Short.MAX_VALUE)))
														.addComponent(
															stueckLabel)
														.addGroup(
															javax.swing.GroupLayout.Alignment.TRAILING,
															layout.createSequentialGroup()
																.addComponent(
																	inszenScrollPane,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	140,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(18,
																	18,
																	18)
																.addGroup(
																	layout.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING)
																		.addGroup(
																			layout.createSequentialGroup()
																				.addGap(0,
																					0,
																					Short.MAX_VALUE)
																				.addGroup(
																					layout.createParallelGroup(
																						javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																							inszenNeuButton,
																							javax.swing.GroupLayout.PREFERRED_SIZE,
																							137,
																							javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																							inszenBearbButton,
																							javax.swing.GroupLayout.PREFERRED_SIZE,
																							137,
																							javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																							inszenEntfButton,
																							javax.swing.GroupLayout.PREFERRED_SIZE,
																							137,
																							javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																							inszenPrHeftButton,
																							javax.swing.GroupLayout.PREFERRED_SIZE,
																							137,
																							javax.swing.GroupLayout.PREFERRED_SIZE)

																				))

																)))
												.addGap(12,
													12,
													12)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
															layout.createSequentialGroup()
																.addComponent(
																	jScrollPane2,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	140,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																	javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																	personenButton))
														.addComponent(
															personenLabel))
												.addGap(6,
													6,
													6)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															ensembleLabel)
														.addGroup(
															layout.createSequentialGroup()
																.addComponent(
																	jScrollPane3,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	140,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																	javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																	layout.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(
																			ensembleDetailButton,
																			javax.swing.GroupLayout.PREFERRED_SIZE,
																			137,
																			javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																			ensembleNeuButton,
																			javax.swing.GroupLayout.PREFERRED_SIZE,
																			137,
																			javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																			ensembleKlonenButton,
																			javax.swing.GroupLayout.PREFERRED_SIZE,
																			137,
																			javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																			ensembleEntfButton,
																			javax.swing.GroupLayout.PREFERRED_SIZE,
																			137,
																			javax.swing.GroupLayout.PREFERRED_SIZE)))))
										.addComponent(
											inszLabel))
								.addGap(0,
									79,
									Short.MAX_VALUE)))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(titleLabel)
				.addGap(21, 21, 21)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(
							stueckLabel,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							26,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(
							personenLabel,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							26,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(
							ensembleLabel,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							26,
							javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING,
								false)
								.addComponent(
									jScrollPane2)
								.addComponent(
									jScrollPane1)
								.addGroup(
									layout.createSequentialGroup()
										.addComponent(
											ladeStueckButton,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											32,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
											detailStueckButton,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											32,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.RELATED,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											Short.MAX_VALUE)
										.addComponent(
											cloneStueckButton,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											32,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
											entferneStueckButton,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											32,
											javax.swing.GroupLayout.PREFERRED_SIZE)))
						.addComponent(
							personenButton,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							32,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(
							jScrollPane3,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							146,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
								.addComponent(
									ensembleDetailButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									32,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									ensembleNeuButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									32,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									ensembleKlonenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									32,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									ensembleEntfButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									32,
									javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									Short.MAX_VALUE)
								.addComponent(
									hilfeButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									43,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									projektButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									43,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									speichernButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									43,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									beendenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									43,
									javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGroup(
							layout.createSequentialGroup()
								.addGap(18, 18,
									18)
								.addComponent(
									inszLabel,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									26,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(9, 9, 9)
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING,
										false)
										.addGroup(
											layout.createSequentialGroup()
												.addComponent(
													inszenNeuButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													32,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(
													inszenBearbButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													32,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addComponent(
													inszenEntfButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													32,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													Short.MAX_VALUE)
												.addComponent(
													inszenPrHeftButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													32,
													javax.swing.GroupLayout.PREFERRED_SIZE))
										.addComponent(
											inszenScrollPane,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											146,
											javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(0,
									84,
									Short.MAX_VALUE)))
				.addGap(7, 7, 7)));

	pack();
    }

    private void erzeugeFileFilter() {
	htmlFileChooser = new JFileChooser("resource");
	htmlFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	htmlFileChooser.setDialogTitle("Stueck-Auswaehlen");
	FileFilter html = new FileFilter() {

	    @Override
	    public String getDescription() {
		return "HTML- Dateien(*.html)";
	    }

	    @Override
	    public boolean accept(File f) {
		return f.getName().toLowerCase().endsWith("html")
			|| f.isDirectory();
	    }
	};
	htmlFileChooser.setFileFilter(html);

	nnnFileChooser = new JFileChooser("demo");
	nnnFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	nnnFileChooser.setDialogTitle("Projekt-Auswaehlen");
	FileFilter nnn = new FileFilter() {

	    @Override
	    public String getDescription() {
		return "Projekt- Dateien(*.nnn)";
	    }

	    @Override
	    public boolean accept(File f) {
		return f.getName().toLowerCase().endsWith("nnn")
			|| f.isDirectory();
	    }
	};
	nnnFileChooser.setFileFilter(nnn);

    }

    @Override
    public void keyPressed(KeyEvent arg0) {
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == ladeStueckButton) {
	    ladeStueck();
	} else if (e.getSource() == hilfeButton) {
	    ladeHilfe();
	} else if (e.getSource() == projektButton) {
	    ladeProjekt();
	} else if (e.getSource() == personenButton) {
	    oeffnePersonDialog();
	} else if (e.getSource() == beendenButton) {
	    programmBeenden();
	} else if (e.getSource() == cloneStueckButton) {
	    // gewaehltes Stueck klonen
	    if (stueckList.getSelectedIndex() > -1) {
		int selectedIndex = stueckList.getSelectedIndex();
		Stueck selectedStueck = Manager.getInstance().getStuecke()
			.get(selectedIndex);
		Manager.getInstance().fuegeStueckHinzu(
			selectedStueck.tiefeKopie());
		Manager.getInstance().fireListChanged();
	    }
	} else if (e.getSource() == detailStueckButton) {
	    if (stueckList.getSelectedIndex() > -1) {
		oeffneStueckDialog();
	    }
	} else if (e.getSource() == entferneStueckButton) {
	    if (stueckList.getSelectedIndex() > -1) {
		int ret = JOptionPane.showConfirmDialog(null,
			"Wollen Sie dieses Stück wirklich entfernen?",
			"Stueck entfernen!", JOptionPane.YES_NO_OPTION);
		if (ret == JOptionPane.YES_OPTION) {
		    Manager.getInstance().getStuecke()
			    .remove(stueckList.getSelectedIndex());
		    Manager.getInstance().fireListChanged();
		}
	    }
	} else if (e.getSource() == ensembleNeuButton) {
	    // erst pruefen, ob ueberhaupt Darsteller/Personen vorhanden sind
	    if (Manager.getInstance().getPersonen().isEmpty()) {
		JOptionPane.showMessageDialog(null,
			"Bitte legen Sie erst Personen an!");
	    } else {
		List<Person> gruppe = new ArrayList<Person>();
		oeffneEnsembleDialog(new Ensemble("neues Ensemble", gruppe));
	    }
	} else if (e.getSource() == ensembleDetailButton) {
	    if (ensembleList.getSelectedIndex() > -1) {
		int selectedIndex = ensembleList.getSelectedIndex();
		oeffneEnsembleDialog(Manager.getInstance().getEnsembles()
			.get(selectedIndex));
	    }
	} else if (e.getSource() == ensembleKlonenButton) {
	    if (ensembleList.getSelectedIndex() > -1) {
		int selectedIndex = ensembleList.getSelectedIndex();
		Manager.getInstance().fuegeEnsembleHinzu(
			Manager.getInstance().getEnsembles().get(selectedIndex)
				.flacheKopie());
		Manager.getInstance().fireListChanged();
	    }
	} else if (e.getSource() == inszenNeuButton) {
	    if (datenfuerInszVorhanden()) {
		oeffneInszenDialog(true);
	    }
	} else if (e.getSource() == inszenBearbButton) {
	    if (inszenList.getSelectedIndex() > -1) {
		if (datenfuerInszVorhanden()) {
		    oeffneInszenDialog(false);
		}
	    }
	} else if (e.getSource() == inszenEntfButton) {
	    if (inszenList.getSelectedIndex() > -1) {
		int ret = JOptionPane.showConfirmDialog(null,
			"Wollen Sie diese Inszenierung wirklich entfernen?",
			"Inszenierung entfernen!", JOptionPane.YES_NO_OPTION);
		if (ret == JOptionPane.YES_OPTION) {
		    Manager.getInstance().getInszenierungen()
			    .remove(inszenList.getSelectedIndex());
		    Manager.getInstance().fireListChanged();
		}
	    }
	} else if (e.getSource() == ensembleEntfButton) {
	    if (ensembleList.getSelectedIndex() > -1) {
		entferneEnsemble(ensembleList.getSelectedIndex());
		Manager.getInstance().fireListChanged();
	    }
	} else if (e.getSource() == inszenPrHeftButton) {
	    if (inszenList.getSelectedIndex() > -1) {
		erzeugeProgrammheft();
	    }
	} else if (e.getSource() == speichernButton) {
	    speichern();
	}
    }

    private void programmBeenden() {
	int ret = JOptionPane.showConfirmDialog(null,
		"Wollen Sie die Änderungen speichern?", "Änderungen sichern!",
		JOptionPane.YES_NO_OPTION);
	if (ret == JOptionPane.YES_OPTION) {
	    speichern();
	} else {
	    Manager.getInstance().entferneSicherung();
	}
	System.exit(0);
    }

    private void speichern() {
	JFileChooser fileChooser = new JFileChooser();
	int ret = fileChooser.showSaveDialog(this);
	if (ret == JFileChooser.APPROVE_OPTION) {
	    Manager.getInstance().schreibeProjektInDatei(
		    fileChooser.getSelectedFile(), false);
	}
    }

    private void erzeugeProgrammheft() {
	if (inszenList.getSelectedIndex() > -1) {
	    try {
		// Prgrammheft als Textdatei ausgeben;
		File file = new File("Programmheft.txt");
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));

		Inszenierung ins = Manager.getInstance().getInszenierungen()
			.get(inszenList.getSelectedIndex());

		writer.write("Programmheft zu:");
		writer.newLine();
		writer.write(ins.getInszBezeichnung() + " - "
			+ ins.getStueck().getAkte().size() + " Akte");
		writer.newLine();
		writer.newLine();
		writer.write("Besetzung der Rollen (Rolle - Schauspieler):");
		for (Darstellerposten d : ins.getDarstellerPosten()) {
		    writer.newLine();
		    writer.write(d.getRolle().getRollenName() + " - "
			    + d.getBesetzung().get(0).getName());
		}
		writer.newLine();
		writer.newLine();
		for (Nichtdarstellerposten n : ins.getNichtDarPosten()) {
		    writer.newLine();
		    writer.write(n.getBezeichnung() + " - ");
		    for (Person p : n.getBesetzung()) {
			writer.write(p.getName() + ",");
		    }
		}
		writer.flush();
		writer.close();
		JOptionPane.showMessageDialog(null,
			"Das Programmheft wurde erstellt!");

	    } catch (IOException e) {
		JOptionPane.showMessageDialog(null,
			"Fehler beim Schreiben des Programmheftes!");
		e.printStackTrace();
	    }
	}
    }

    private void entferneEnsemble(int selectedIndex) {
	// Test, ob Ensemble bei einer Inszenierung
	// verwendet wird
	String insz = "";
	Ensemble ensemble = Manager.getInstance().getEnsembles()
		.get(selectedIndex);
	if (!Manager.getInstance().ensembleWirdVerwendet(ensemble)) {
	    int ret = JOptionPane.showConfirmDialog(null,
		    "Wollen Sie dieses Ensemble wirklich entfernen?",
		    "Ensemble entfernen!", JOptionPane.YES_NO_OPTION);
	    if (ret == JOptionPane.YES_OPTION) {
		Manager.getInstance().getEnsembles().remove(selectedIndex);
	    }
	} else {
	    for (int i = 0; i < Manager.getInstance().getInszenierungen()
		    .size(); i++) {
		if (Manager.getInstance().getInszenierungen().get(i)
			.getEnsemble().getName().equals(ensemble.getName())) {
		    insz = Manager.getInstance().getInszenierungen().get(i)
			    .getInszBezeichnung();
		}
	    }
	    JOptionPane
		    .showMessageDialog(
			    null,
			    "Dieses Ensembel wird bei der Inszenierung \""
				    + insz
				    + "\" verwendet\nund kann daher nicht entfernt werden!");
	}
    }

    private void oeffneStueckDialog() {
	int selectedIndex = stueckList.getSelectedIndex();
	Stueck selectedStueck = Manager.getInstance().getStuecke()
		.get(selectedIndex);
	// es duerfen nur Kopien editiert werden;
	if (selectedStueck.getName().contains("-")
		|| selectedStueck.getName().contains("Kopie")) {
	    final StueckDialog stueckDialog = new StueckDialog(selectedStueck);
	    stueckDialog.setModal(true);
	    stueckDialog.setLocationRelativeTo(null);
	    stueckDialog.setVisible(true);
	} else {
	    JOptionPane
		    .showMessageDialog(
			    null,
			    "Dieses Original kann nicht bearbeitet werden.\nBitte erstellen Sie mit \"Stueck klonen\" erst eine Kopie\noder verwenden Sie die bereits vorhandene Kopie!");
	}

    }

    private boolean datenfuerInszVorhanden() {
	// eine Inszenierung kann erst angelegt werden,
	// wenn ein Ensemble und Theaterstuecke vorhanden sind;
	if (Manager.getInstance().getStuecke().isEmpty()) {
	    JOptionPane.showMessageDialog(null,
		    "Bitte lesen Sie erst Theaterstuecke ein!");
	    return false;
	} else if (Manager.getInstance().getEnsembles().isEmpty()) {
	    JOptionPane.showMessageDialog(null,
		    "Bitte erzeugen Sie erst Ensembles!");
	    return false;
	}
	return true;
    }

    private void oeffneInszenDialog(boolean neu) {
	if (neu) {
	    final InszStueckDialog inszStueckDialog = new InszStueckDialog();
	    inszStueckDialog.setModal(true);
	    inszStueckDialog.setLocationRelativeTo(null);
	    inszStueckDialog.setVisible(true);

	} else {
	    if (inszenList.getSelectedIndex() > -1) {
		Inszenierung insz = Manager.getInstance().getInszenierungen()
			.get(inszenList.getSelectedIndex());
		final InszenierungDialog inszDialog = new InszenierungDialog(
			insz);
		inszDialog.setModal(true);
		inszDialog.setLocationRelativeTo(null);
		inszDialog.setVisible(true);
	    } else {
		JOptionPane.showMessageDialog(null,
			"Bitte erst eine Inszenierung anwaehlen!");
	    }

	}
    }

    private void oeffneEnsembleDialog(Ensemble ensemble) {
	final EnsembleDialog ensDialog = new EnsembleDialog(ensemble);
	ensDialog.setModal(true);
	ensDialog.setLocationRelativeTo(null);
	ensDialog.setVisible(true);
    }

    private void oeffnePersonDialog() {
	final PersonDialog personenDialog = new PersonDialog();
	personenDialog.setModal(true);
	personenDialog.setLocationRelativeTo(null);
	personenDialog.setVisible(true);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void fillPersonList() {
	// wenn Personen vorhanden, dann die Liste damit fuellen
	DefaultListModel daten = new DefaultListModel();
	if (!Manager.getInstance().getPersonen().isEmpty()) {
	    for (int i = 0; i < Manager.getInstance().getPersonen().size(); i++) {
		daten.add(i, Manager.getInstance().getPersonen().get(i)
			.getName());
	    }
	}
	personList.setModel(daten);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void fillStueckList() {
	// analog zu fillPersonList();
	DefaultListModel daten = new DefaultListModel();
	if (!Manager.getInstance().getStuecke().isEmpty()) {
	    for (int i = 0; i < Manager.getInstance().getStuecke().size(); i++) {
		daten.add(i, Manager.getInstance().getStuecke().get(i)
			.getName());
	    }
	}
	stueckList.setModel(daten);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void fillEnsembleList() {
	// analog zu fillPersonList();
	DefaultListModel daten = new DefaultListModel();
	if (!Manager.getInstance().getEnsembles().isEmpty()) {
	    for (int i = 0; i < Manager.getInstance().getEnsembles().size(); i++) {
		daten.add(i, Manager.getInstance().getEnsembles().get(i)
			.getName());
	    }
	}
	ensembleList.setModel(daten);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void fillInszenList() {
	// analog zu fillPersonList();
	DefaultListModel daten = new DefaultListModel();
	if (!Manager.getInstance().getInszenierungen().isEmpty()) {
	    for (int i = 0; i < Manager.getInstance().getInszenierungen()
		    .size(); i++) {
		daten.add(i, Manager.getInstance().getInszenierungen().get(i)
			.getInszBezeichnung());
	    }
	}
	inszenList.setModel(daten);
    }

    private void ladeHilfe() {
	try {
	    Desktop.getDesktop().open(new File("resource/Hilfe.pdf"));
	} catch (IOException e1) {
	    e1.printStackTrace();
	    JOptionPane.showMessageDialog(null,
		    "Beim Laden der Hilfe ist ein Fehler aufgetreten!");
	}
    }

    private void ladeStueck() {
	int ret = htmlFileChooser.showOpenDialog(this);
	if (ret == JFileChooser.APPROVE_OPTION) {
	    File file = htmlFileChooser.getSelectedFile();
	    Manager.getInstance().ladeStueck(file.getPath());
	} else {
	    dispose();
	}
    }

    @Override
    public void valueChanged(ListSelectionEvent arg0) {
    }

    @Override
    public void listChanged() {
	fillEnsembleList();
	fillInszenList();
	fillPersonList();
	fillStueckList();
    }

}
